package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.core.constants.Constants;
import com.blt.govdata.model.TProjectProblem;
import com.blt.govdata.model.TProjectProgress;
import com.blt.govdata.service.ITProjectProblemService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tProjectProblem")
//todo 将所有的XXX修改为真实值
public class TProjectProblemController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITProjectProblemService service;

    /**
     * 根据项目ID查询项目存在的问题
     */

    @RequestMapping(value = "/selectPageByPid", params = {"Pid"})
    public Map<String, Object> selectPageByPid(Integer Pid, Page page) {
        Page<TProjectProgress> result = service.selectPage(page, BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", Pid)
                .and("is_delete = {0}", Constants.UN_DELETED)
                .and("examine_state = {0}", Constants.AUDITED_PASS));
        logger.info("分页根据项目ID查询项目问题");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 新增项目问题
     */
    @RequestMapping(value = "/addProjectProblem")
    public Map<String, Object> addProjectProblem(TProjectProblem tProjectProblem) {
        if (null == tProjectProblem || tProjectProblem.getpId() == null)
            return ImmutableMap.of("status", "false", "data", false);
        boolean result = this.service.insert(tProjectProblem);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 编辑项目问题
     */
    @RequestMapping(value = "/editProjectProblem")
    public Map<String, Object> editProjectProblem(TProjectProblem tProjectProblem) {
        if (null == tProjectProblem || tProjectProblem.getId() == null)
            return ImmutableMap.of("status", "false", "data", false);
        boolean result = this.service.updateById(tProjectProblem);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 删除项目问题
     */
    @RequestMapping(value = "/deleteProjectProblem", params = {"id"})
    public Map<String, Object> deleteProjectProblem(Integer id) {
        TProjectProblem tProjectProblem = BeanUtils.instantiate(TProjectProblem.class);
        tProjectProblem.setId(id);
        tProjectProblem.setIsDelete(0);
        boolean result = this.service.updateById(tProjectProblem);
        return ImmutableMap.of("status", "true", "data", result);
    }


    /**
     * 根据ID查询项目问题
     */
    @RequestMapping(value = "/selectProjectProblemById", params = {"id"})
    public Map<String, Object> selectProjectProblemById(Integer id) {
        return ImmutableMap.of("status", "true", "data", this.service.selectById(id));
    }


}
